package lab9p1;

/**
 * RectangleGUI - a graphical user interface program for keeping track of rectangles.
 * 
 * @author
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

public class RectangleGUI extends JFrame implements ActionListener {
	/**
	 * Data members
	 */
	private static final long serialVersionUID = 1L;
	private final int FRAME_X=100, FRAME_Y=100, FRAME_WIDTH=400,  FRAME_HEIGHT=300;
	private final int SCROLL_X=30, SCROLL_Y=30, SCROLL_WIDTH=300, SCROLL_HEIGHT=200;
	private final int SORT_X=30,   SORT_Y=235,  SORT_WIDTH=100,   SORT_HEIGHT=30;

	private JList<Rectangle> listview;
	private JScrollPane scroll;
	
	private DefaultListModel<Rectangle> listmodel;
	private JButton sort;
	private String SORT_STRING = "Sort";
	private String SHUFFLE_STRING = "Shuffle";

	/*
	 * Menu for saving and reading files
	 */
	private JMenuItem newMenuItem;
	private JMenuItem saveMenuItem;
	private JFileChooser filechooser = new JFileChooser();

	/**
	 * RectangleGUI constructor
	 * 
	 * @param title
	 *
	 */
	public RectangleGUI(String title) {
		super(title);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout( null );

		/*
		 * Set up List of rectangles
		 */
		setupList();
		
		/*
		 * Add a sort button
		 */
		sort = new JButton( SORT_STRING );
		sort.addActionListener( this );
		sort.setBounds(SORT_X, SORT_Y, SORT_WIDTH, SORT_HEIGHT);
		add(sort);

		/*
		 * Add menus 
		 */
		setJMenuBar( setupMenu() );
		
		setVisible( true );
	}

	/**
	 * actionPerformed - 
	 * 
	 * 
	 */
	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		
		/*
		 * Sort button
		 */
		if (source.equals(sort)){
			/*
			 * Copy the data in the JList into a Vector of Rectangles so that we can sort it
			 */
			Vector<Rectangle> v = new Vector<Rectangle>();
			for (int i=0; i<listmodel.getSize(); i++)
				v.add( (Rectangle) (listmodel.elementAt( i )) );
			if (sort.getText().equals( SORT_STRING )){
				sort.setText(SHUFFLE_STRING);
				/*
				 * Sort
				 */
				Collections.sort( v );
			} else {
				if (sort.getText().equals( SHUFFLE_STRING )){
					sort.setText( SORT_STRING );
				/*
				 * Shuffle
				 */
				Collections.shuffle( v );
				}
			}
			listmodel.clear();
			for (Rectangle s : v)
				listmodel.addElement( s );
		}
		
		/*
		 * Menu bar
		 */
		if (source.equals( newMenuItem ) ){
			listmodel.clear();
		} else if(source.equals( saveMenuItem ) ){
			if (filechooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
				File file = filechooser.getSelectedFile();

				try {
					ObjectOutputStream oos = new ObjectOutputStream( new PrintStream( file ));
					oos.writeObject( listmodel );
					oos.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}  
	}

	/**
	 * setupList
	 */
	private void setupList(){
		/*
		 * Data that will be used as the "List Model"
		 * Create rectangles of various random sizes
		 */
		final int NUMBER_OF_RECTANGLES = 22;
		final int MAX_SIZE = 100;
		Rectangle[] rectangles = new Rectangle[ NUMBER_OF_RECTANGLES ];
		for (int i=0; i<NUMBER_OF_RECTANGLES; i++){
			int length = (int)(Math.random()*MAX_SIZE);
			int width  = (int)(Math.random()*MAX_SIZE);
			rectangles[i] = new Rectangle( length, width );
		}
		
		/*
		 * A List model is used to store data that will be displayed by JList
		 */
		listmodel = new DefaultListModel<Rectangle>();
		for (Rectangle s : rectangles)
			listmodel.addElement( s );

		/*
		 * Now that the data is available, set up GUI elements to display it.
		 */
		listview = new JList<Rectangle>( listmodel );
		listview.setVisible(true);
		scroll = new JScrollPane( listview );
		scroll.setBounds(SCROLL_X, SCROLL_Y, SCROLL_WIDTH, SCROLL_HEIGHT);
		add( scroll );		
	}
	
	/**
	 * setupMenu
	 * @return
	 */
	private JMenuBar setupMenu(){
		final String FILE_MENU = "File";
		final String NEW_MENU_STRING = "New";
		final String OPEN_MENU_STRING = "Open";
		final String SAVE_MENU_STRING = "Save";
		
		JMenuBar bar = new JMenuBar();
		JMenu fmenu = new JMenu( FILE_MENU );
		
		newMenuItem = new JMenuItem( NEW_MENU_STRING );
		newMenuItem.addActionListener( this );
		saveMenuItem = new JMenuItem( SAVE_MENU_STRING );
		saveMenuItem.addActionListener( this );
		
		fmenu.add( newMenuItem );
		fmenu.add( saveMenuItem );
		
		bar.add( fmenu );
		return bar;
	}
	/**
	 * main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new RectangleGUI("My Rectangles");
	}
}
